package ywpt.web.action.service;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;

import org.ecside.table.limit.Limit;
import org.ecside.util.RequestUtils;
import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Restrictions;

import ywpt.components.acegi.domain.Operation;
import ywpt.core.Constants;
import ywpt.core.SystemMessage;
import ywpt.core.web.BaseAction;
import ywpt.model.SmallAmount;
import ywpt.model.SysMsgInfo;
import ywpt.service.service.SmallAmountService;
import ywpt.service.work.SysMsgInfoService;
import ywpt.web.common.OperationConstants;
import ywpt.web.common.PublicFunction;

import com.opensymphony.xwork2.ModelDriven;
import com.opensymphony.xwork2.Preparable;

public class SmallAmountCheckAction extends BaseAction implements ModelDriven<SmallAmount>,Preparable{
	/**
	 * 小额收款审核
	 */
	private static final long serialVersionUID = 5500632947913118891L;
	private static final int DEFAULT_PAGE_SIZE = 100;	
	private boolean[] popedom=new boolean[Constants.POPEDOM_LEN];	
	private Long id;
	private SmallAmount smallAmount;
	private List<SmallAmount> smallAmountList = new ArrayList<SmallAmount>();
	private SmallAmountService smallAmountService;
	
	private int version;
	private Long jobId;
	private String jobName;
	private Long repairId;
	private String repairName;
	private Long clientId;
	private String clientName;
	private Long checkId;
	private String checkName;

	private List<SysMsgInfo> sysMsgInfoList = new ArrayList<SysMsgInfo>();
	private SysMsgInfoService sysMsgInfoService;
	public List<SysMsgInfo> getSysMsgInfoList() {
		return sysMsgInfoList;
	}

	public void setSysMsgInfoList(List<SysMsgInfo> sysMsgInfoList) {
		this.sysMsgInfoList = sysMsgInfoList;
	}

	public SysMsgInfoService getSysMsgInfoService() {
		return sysMsgInfoService;
	}

	public void setSysMsgInfoService(SysMsgInfoService sysMsgInfoService) {
		this.sysMsgInfoService = sysMsgInfoService;
	}

	public boolean[] getPopedom() {
		return popedom;
	}

	public void setPopedom(boolean[] popedom) {
		this.popedom = popedom;
	}

	public Long getId() {
		return id;
	}

	public void setId(Long id) {
		this.id = id;
	}

	public SmallAmount getSmallAmount() {
		return smallAmount;
	}

	public void setSmallAmount(SmallAmount smallAmount) {
		this.smallAmount = smallAmount;
	}

	public List<SmallAmount> getSmallAmountList() {
		return smallAmountList;
	}

	public void setSmallAmountList(List<SmallAmount> smallAmountList) {
		this.smallAmountList = smallAmountList;
	}

	public SmallAmountService getSmallAmountService() {
		return smallAmountService;
	}

	public void setSmallAmountService(SmallAmountService smallAmountService) {
		this.smallAmountService = smallAmountService;
	}

	public int getVersion() {
		return version;
	}

	public void setVersion(int version) {
		this.version = version;
	}

	public Long getJobId() {
		return jobId;
	}

	public void setJobId(Long jobId) {
		this.jobId = jobId;
	}

	public String getJobName() {
		return jobName;
	}

	public void setJobName(String jobName) {
		this.jobName = jobName;
	}

	public Long getRepairId() {
		return repairId;
	}

	public void setRepairId(Long repairId) {
		this.repairId = repairId;
	}

	public String getRepairName() {
		return repairName;
	}

	public void setRepairName(String repairName) {
		this.repairName = repairName;
	}

	public Long getClientId() {
		return clientId;
	}

	public void setClientId(Long clientId) {
		this.clientId = clientId;
	}

	public String getClientName() {
		return clientName;
	}

	public void setClientName(String clientName) {
		this.clientName = clientName;
	}

	public Long getCheckId() {
		return checkId;
	}

	public void setCheckId(Long checkId) {
		this.checkId = checkId;
	}

	public String getCheckName() {
		return checkName;
	}

	public void setCheckName(String checkName) {
		this.checkName = checkName;
	}

	//-------------------------------------------------------------------------------------------------	
	@Override
	public SmallAmount getModel() {
		return this.smallAmount;
	}

	@Override
	public void prepare() throws Exception {
		if (id!=null) {
			this.smallAmount = this.smallAmountService.getSmallAmountById(id);
		} else {
			this.smallAmount = new SmallAmount();
		}
		HashMap<String,Operation> oper=this.getOperMapByUser(this.getCurrentUser());
		PublicFunction.getOperationPopedom(oper, popedom, OperationConstants.SERVICE_AMOUNT_CHECK);				
	}
	
	protected void getDataList(DetachedCriteria query) {
		query.createAlias("checker", "checker").add(Restrictions.eq("checker.id", this.getCurrentUser().getPeople().getId()));
		query.add(Restrictions.eq("submitFlag", true));
		query.add(Restrictions.eq("deleteFlag", false));
		int totalRows = RequestUtils.getTotalRowsFromRequest(getRequest());
		if (totalRows < 0) {
			totalRows = smallAmountService.getSmallAmountsCountByCriteria(query);
		}
		int[] rowStartEnd = RequestUtils.getRowStartEnd(getRequest(),
				totalRows, DEFAULT_PAGE_SIZE, 0);
		Limit limit = RequestUtils.getLimit(getRequest(), "ec", totalRows,
				DEFAULT_PAGE_SIZE);
		int pageSize = limit.getCurrentRowsDisplayed();
		query.addOrder(Order.desc("id"));
		query.setProjection(null);
		smallAmountList.addAll(smallAmountService.getSmallAmounts(query, pageSize, rowStartEnd[0]));
	}
	
	protected void showInfo() {
		if (this.smallAmount.getJobInfo()!=null) {
			this.jobId = this.smallAmount.getJobInfo().getId();
			this.jobName =this.smallAmount.getJobInfo().getJobCode();
		}
		if (this.smallAmount.getRepairInfo()!=null) {
			this.repairId =  this.smallAmount.getRepairInfo().getId();
			this.repairName = this.smallAmount.getRepairInfo().getStr();
		}
		if (this.smallAmount.getClientInfo()!=null) {
			this.clientId = this.smallAmount.getClientInfo().getId();
			this.clientName = this.smallAmount.getClientInfo().getClientName();
		}
		if (this.smallAmount.getChecker()!=null) {
			this.checkId = this.smallAmount.getChecker().getId();
			this.checkName = this.smallAmount.getChecker().getPeopleName();
		}
		this.sysMsgInfoList.addAll(this.sysMsgInfoService.getMsgs(2, this.smallAmount.getId()));
	}
	//-------------------------------------------------------------------------------------------------	
	
	public String list() {
		DetachedCriteria query=DetachedCriteria.forClass(SmallAmount.class);
		getDataList(query);
		return SUCCESS;
	}
	
	public String edit() {
		this.smallAmount = this.smallAmountService.getSmallAmountById(id);
		showInfo();
		return INPUT;
	}	
	
	public String disSubmit() {
		if (id==null) {
			this.addActionMessage(SystemMessage.NO_OBJECT);
		} else {
			this.smallAmount = this.smallAmountService.getSmallAmountById(id);
			if (!smallAmount.getCheckFlag()) {
				if (smallAmount.getSubmitFlag()) {
					smallAmount.setSubmitFlag(false);
					smallAmountService.updateSmallAmount(smallAmount, null, null, null, null);
					sysMsgInfoService.invalidMsg(2, id);
					addDisSubmitSysMsg(smallAmount);
					this.addActionMessage(SystemMessage.DISSUMBIT_SUCCESS);
				} else {
					this.addActionMessage("当前记录尚未提交！");
				}
			} else {
				this.addActionMessage(SystemMessage.AUDITING_SAVE);
			}
		}
		this.showInfo();
		return SUCCESS;
	}
	
	public String check() {
		if(id!=null){
			this.smallAmount = this.smallAmountService.getSmallAmountById(id);
			if(!smallAmount.getCheckFlag()){
				if (smallAmount.getSubmitFlag()) {
					smallAmount.setCheckFlag(true);
					this.smallAmountService.updateSmallAmount(smallAmount, null, null, null, null);
					sysMsgInfoService.invalidMsg(2, id);
					this.addActionMessage(SystemMessage.AUDITING_SUCCESS);					
				} else 
					this.addActionMessage("当前记录尚未提交，不能审核！");
			}else
				this.addActionError(SystemMessage.AUDITING_REPEAT);
		}else
			this.addActionMessage(SystemMessage.NO_OBJECT);
		showInfo();
		return SUCCESS;
	}
	
	public String disCheck() {
		if(id!=null){
			this.smallAmount = this.smallAmountService.getSmallAmountById(id);
			if(smallAmount.getCheckFlag()){
				smallAmount.setCheckFlag(false);
				this.smallAmountService.updateSmallAmount(smallAmount, null, null, null, null);
				this.addActionMessage(SystemMessage.DISAUDITING_SUCCESS);
			}else
				this.addActionError(SystemMessage.DISAUDITING_REPEAT);
		}else
			this.addActionMessage(SystemMessage.NO_OBJECT);
		showInfo();
		return SUCCESS;

	}

	protected void addDisSubmitSysMsg(SmallAmount smallAmount) {
		SysMsgInfo sysMsgInfo = new SysMsgInfo();
		sysMsgInfo.setMsgTitle("小额收款");
		sysMsgInfo.setMsgContent(smallAmount.getChecker().getPeopleName()+"退回了一条小额收款信息，等待处理！");
		sysMsgInfo.setReceiveUser(smallAmount.getCreater().getId() +",");
		sysMsgInfo.setBusinessType(2);
		sysMsgInfo.setBusinessId(id);
		sysMsgInfo.setUrl("/small/amount/edit-"+id+".action");
		sysMsgInfo.setCreateDate(new Date());
		sysMsgInfo.setCreater(this.getCurrentUser());
		sysMsgInfoService.saveSysMsgInfo(sysMsgInfo);
	}
}
